import socket
import time
import random

# 服务端地址和端口
SERVER_HOST = '127.0.0.1'
SERVER_PORT = 8888

# 创建 TCP 客户端套接字
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((SERVER_HOST, SERVER_PORT))

try:
    while True:
        # 生成随机数（范围可自定义）
        random_num = random.randint(0, 100)
        # 转换为字节流发送
        client_socket.send(str(random_num).encode('utf-8'))
        print(f"已发送: {random_num}")
        # 每秒发送一次
        time.sleep(1)
except KeyboardInterrupt:
    print("客户端停止")
finally:
    client_socket.close()